<?php
declare (strict_types = 1);
namespace app\admin\controller;
use app\admin\model\User;
use app\admin\model\LoginLog;
use liliuwei\think\Jump;
use think\captcha\facade\Captcha;
use think\facade\View;
use think\Request;
use think\facade\Session;
use app\admin\validate\User as UserValidate;
class Login
{
    use Jump;

    /**
     * 显示资源列表
     * @return \think\Response
     */
    public function index(Request $request)
    {
		
        event('UserLogin');
        // 判断是否登录
        if (get_admin_session()) {
            # code...
            // 重定向
            $this->redirect("/admin/index"); 
        }

        if ($request->isPost()){
           

            // 验证器使用：Validate
            // 路径：\app\admin\validate\User.php
            $userValidate = new UserValidate;
            if (!$userValidate->check($request->param(['username','password','vercode']))){
                return $this->error($userValidate->getError());
            }
			 // 验证码验证，因使用了上面验证器验证，所以注释下面的
            // if (!Captcha::check($request->param('vercode'))){
            //     return $this->error('验证码错误','/admin/login');
            // }

           $userFlag =  User::Where(['username'=> $request->param('username'),'password' =>md5($request->param('password')),'status'=> 1])
               ->withoutField('password,phone')->find();
            
            if (is_null($userFlag)){
                return $this->error('账号密码错误，请重新登录');
            }else{
                // 登录log
                // $ip=getRealIP();
                $ip='113.94.50.77';
                // 根据IP获取省份城市
                $ips=getip($ip);
                // 省份
                $province=$ips['province'];
                // 城市
                $city=$ips['city'];
                // 登录设备
                $equipment=getEquipment($_SERVER['HTTP_USER_AGENT']);
                // 调用loginlog方法记录登录信息
                $this->loginlog($userFlag['id'],$userFlag['username'],$ip,$province,$city,$equipment);
                Session::set('name',$userFlag->toArray());
                // 多语言使用
                return $this->success(lang('Login_successful'));
            }

        }else{
            return View::fetch("login");
			
        }

    }

    /**
     * @name: 
     * @Description: 描述：验证码
     * @param {*}
     * @return {*}
     * @Date: 2021-06-18 13:22:05
     * @LastEditTime: Do not edit
     * @Route: 
     */    
    public function verify()
    {
        return Captcha::create();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 退出登录
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        clear_admin_session();
        return $this->success('退出成功','./login');
    }

    /**
     * @name: 
     * @Description: 描述：记录登录日志
     * @param {*} $userId
     * @param {*} $username
     * @param {*} $ip
     * @param {*} $province
     * @param {*} $city
     * @param {*} $equipment
     * @return {*}
     * @Date: 2022-04-03 21:11:01
     * @LastEditTime: Do not edit
     * @Route: 
     */    
    public function loginlog($userId,$username,$ip,$province,$city,$equipment){
        $LoginLog = new LoginLog;
        $LoginLog->save([
            'userid'  =>  $userId,
            'username'=>  $username,
            'ip'=>  $ip,
            'province'=>  $province,
            'city'=>  $city,
            'equipment'=>  $equipment,
        ]);
    }

    /**
     * @name: 
     * @Description: 描述：找回密码页面
     * @param {*}
     * @return {*}
     * @Date: 2022-04-03 21:10:36
     * @LastEditTime: Do not edit
     * @Route: 
     */    
    public function forgotPassWord(){
        return view::fetch('forget');
    }

  

}

